package z.sample.facade;

import java.util.ArrayList;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;

import z.sample.persistence.entity.Customer;
import z.sample.persistence.entity.Employee;
import z.sample.persistence.entity.Office;
import z.sample.service.DemoService;
import z.sample.servicecommons.ObjectConverter;
import z.sample.servicecommons.dto.CustomerDTO;
import z.sample.servicecommons.dto.EmployeeDTO;
import z.sample.servicecommons.dto.OfficeDTO;
import z.sample.servicecommons.service.DemoRSService;

public class DemoServiceFacade implements DemoRSService {
	@Autowired
	private DemoService demoService;

	@Autowired
	private ObjectConverter converter;
	
	@Override
	public List<CustomerDTO> getAllCustomers() {
		List<CustomerDTO> customerDTOs = new ArrayList<CustomerDTO>();
		List<Customer> customers = this.demoService.getAllCustomers();
		
		for (Customer customer : customers) {
			CustomerDTO customerDTO = null;
			customerDTO = (CustomerDTO) this.converter.convert(customer, new CustomerDTO());
			
			customerDTOs.add(customerDTO);
		}
		
		return customerDTOs;
	}
	
	@Override
	public List<CustomerDTO> findCustomersByName(String name) {
		List<CustomerDTO> customerDTOs = new ArrayList<CustomerDTO>();
		List<Customer> customers = this.demoService.findCustomersByName(name);
		
		for (Customer customer : customers) {
			CustomerDTO customerDTO = null;
			customerDTO = (CustomerDTO) this.converter.convert(customer, new CustomerDTO());
			
			customerDTOs.add(customerDTO);
		}
		
		return customerDTOs;
	}

	@Override
	public List<EmployeeDTO> getAllEmployees() {
		List<EmployeeDTO> employeeDTOs = new ArrayList<EmployeeDTO>();
		List<Employee> employees = this.demoService.getAllEmployees();
		
		for (Employee employee : employees) {
			EmployeeDTO employeeDTO = null;
			employeeDTO = (EmployeeDTO) this.converter.convert(employee, new EmployeeDTO());
			
			employeeDTOs.add(employeeDTO);
		}
		
		return employeeDTOs;
	}

	@Override
	public List<OfficeDTO> getAllOffices() {
		List<OfficeDTO> officeDTOs = new ArrayList<OfficeDTO>();
		List<Office> offices = this.demoService.getAllOffices();
		
		for (Office office : offices) {
			OfficeDTO officeDTO = null;
			officeDTO = (OfficeDTO) this.converter.convert(office, new OfficeDTO());
			
			officeDTOs.add(officeDTO);
		}
		
		return officeDTOs;
	}
	
	@Override
	public List<CustomerDTO> getCustomerOfAReporter(Long reporterId) {
		List<CustomerDTO> customerDTOs = new ArrayList<CustomerDTO>();
		List<Customer> customers = this.demoService.getCustomerOfAReporter(reporterId);
		
		for (Customer customer : customers) {
			CustomerDTO customerDTO = null;
			customerDTO = (CustomerDTO) this.converter.convert(customer, new CustomerDTO());
			
			customerDTOs.add(customerDTO);
		}
		
		return customerDTOs;
	}
}
